package net.realsee.common.utils;

import cn.hutool.core.util.ObjectUtil;
import net.realsee.common.enums.wcs.plc.PlcCommTypeEnum;
import net.realsee.common.enums.wcs.plc.PlcReadWriteErrEnum;
import net.realsee.domain.wcs.device.bo.PlcReadWriteBo;
import net.realsee.framework.plc.IPlcReadWrite;
import net.realsee.framework.plc.PlcOperateResult;

/**
 * PLC工具类
 *
 * @author 孙正华
 * @since 2023/8/17 13:52
 */
public class PlcUtil {

    /**
     * 读取PLC
     */
    public static PlcOperateResult read(PlcReadWriteBo readWriteBo) {
        if (ObjectUtil.isNull(readWriteBo)) {
            return new PlcOperateResult(PlcReadWriteErrEnum.TAG_NOT_EXIST);
        }
        IPlcReadWrite handleService = PlcCommTypeEnum.getHandleService(readWriteBo.getCommType());
        if (ObjectUtil.isNotNull(handleService)) {
            return handleService.read(readWriteBo);
        }
        return new PlcOperateResult(PlcReadWriteErrEnum.NOT_FOUND_HANDLE_SERVICE);
    }

    /**
     * 写入PLC
     */
    public static PlcOperateResult write(PlcReadWriteBo readWriteBo) {
        if (ObjectUtil.isNull(readWriteBo)) {
            return new PlcOperateResult(PlcReadWriteErrEnum.TAG_NOT_EXIST);
        }
        IPlcReadWrite handleService = PlcCommTypeEnum.getHandleService(readWriteBo.getCommType());
        if (ObjectUtil.isNotNull(handleService)) {
            return handleService.write(readWriteBo);
        }
        return new PlcOperateResult(PlcReadWriteErrEnum.NOT_FOUND_HANDLE_SERVICE);
    }

    /**
     * 监听PLC
     */
    public static PlcOperateResult monitor() {
        return null;
    }

    /**
     * 销毁设备的数据交互类
     */
    public static void destroy(PlcReadWriteBo readWriteBo) {
        if (ObjectUtil.isNull(readWriteBo)) {
            return;
        }
        IPlcReadWrite handleService = PlcCommTypeEnum.getHandleService(readWriteBo.getCommType());
        if (ObjectUtil.isNotNull(handleService)) {
            handleService.destroy(readWriteBo.getDevice());
        }
    }

}
